﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace Emoney.UserTraining.Comm
{
    public class HtmlString : IHtmlString
    {
        string html = "";
        public HtmlString()
        {

        }

        public void SetHtml(string html)
        {
            this.html = html;
        }

        public string ToHtmlString()
        {
            return html;
        }
    }

    public class StringTools
    {
        public static string GetStaticFile(string file)
        {
            var str = System.Configuration.ConfigurationManager.AppSettings["StaticFile"];
            if (str == "~/")
            {
                UrlHelper he = new UrlHelper(System.Web.HttpContext.Current.Request.RequestContext);
                string path = he.Content("~/" + file);
                return path;
            }
            else
            {
                return str + file;
            }
        }

        public static string Request(string key)
        {
            var str = System.Web.HttpContext.Current.Request[key];
            return System.Web.HttpUtility.UrlDecode(str);
        }

        public static IHtmlString GetHtmlString(string text)
        {
            HtmlString s = new HtmlString();
            s.SetHtml(text);
            return s;
        }
        public static string SubStr(string str, int len)
        {
            string strrel = str;
            if (str.Length > len)
            {
                strrel = str.Substring(0, len);
                if (len > 3)
                {
                    strrel += "..";
                }
            }
            return strrel;
        }
        public static string GetTeacherImg(string img,int size)
        {
            string strimg = StringTools.GetStaticFile("Images/Train/images/tea" + size + ".jpg");
            if (!string.IsNullOrEmpty(img))
            {
                strimg = img;
                if (size == 39)
                {
                    strimg = "http://i.ymcps.com/BugView/" + img;
                }
            }
            return strimg;
        }
        public static string getTimeIsOpen( string before_time,string class_time,string end_time,string duration_date,string msg_id)
        {    
            string rel = "";
            if (string.IsNullOrEmpty(before_time) && string.IsNullOrEmpty(class_time) && string.IsNullOrEmpty(end_time) && string.IsNullOrEmpty(duration_date) && string.IsNullOrEmpty(msg_id))
            {
                return "";
            }
            else
            {
                DateTime a = DateTime.Now;
                DateTime newtime = Convert.ToDateTime(class_time).AddMinutes(-Convert.ToDouble(before_time));//会议开始时间
                DateTime endtime = Convert.ToDateTime(class_time).AddMinutes(Convert.ToDouble(duration_date));//结束时间
                if (newtime < DateTime.Now && endtime > DateTime.Now)
                {
                    rel = "0";//正在进行
                }
                else if (DateTime.Now > endtime)
                {
                    rel = "-1";//历史会议
                }
                else
                {
                    DateTime classdate = Convert.ToDateTime(class_time);
                    System.TimeSpan TS = new System.TimeSpan(classdate.Ticks - DateTime.Now.Ticks);
                    int day = (int)TS.TotalDays;
                    int hours = (int)TS.Hours;
                    int Minutes = (int)TS.Minutes;
                    if (day > 0)
                    {
                        rel = "<span class=\"red fw\">" + day + "</span>天后开课";
                    }
                    else if (hours > 0)
                    {
                        rel = "<span class=\"red fw\">" + hours + "</span>小时后开课";
                    }
                    else if (Minutes > 0)
                    {
                        rel = "<span class=\"red fw\">" + Minutes + "</span>分钟后开课";
                    }
                    else
                    {
                        rel = "-1";//历史会议
                    }
                }
            }
            return rel;
            
        }
        public static string getTimeIsOpen(string mtg_id, string id, string before_time, string Duration_date)
        {
            if (before_time == null || before_time == "" || Duration_date == null || Duration_date == "")
            {
                return "";
            }

            if ((System.DateTime.Now - Convert.ToDateTime(before_time)).TotalSeconds >= 0 && (Convert.ToDateTime(Duration_date) - System.DateTime.Now).TotalSeconds >= 0)
            {
                return "<a href='#' onclick=\"f_openMeeting('" + mtg_id + "','" + id + "','" + before_time + "','" + Duration_date + "')\" style=\"color: Red\">进入会议</a>";
            }
            else if ((System.DateTime.Now - Convert.ToDateTime(before_time)).TotalSeconds < 0)
            {
                return "未开始";
            }
            else
            {
                return "已结束";
            }

        }

        public static string getCloseOpen(string mtg_id, string id, string before_time, string Duration_date)
        {
            if (before_time == null || before_time == "" || Duration_date == null || Duration_date == "")
            {
                return "";
            }

            if ((System.DateTime.Now - Convert.ToDateTime(before_time)).TotalSeconds >= 0 && (Convert.ToDateTime(Duration_date) - System.DateTime.Now).TotalSeconds >= 0)
            {
                return "<a href=\"#\" onclick=\"closeNetworkMeeting(" + id + ");\" style=\"color: Red\">结束会议</a>";
            }
            else if ((System.DateTime.Now - Convert.ToDateTime(before_time)).TotalSeconds < 0)
            {
                return "";
            }
            else
            {
                return "";
            }
        }

        public static string PageUrl(int recordCount, int index, int pageSize)
        {
            //先计算总页数
            int pageCount = (int)Math.Ceiling((double)recordCount / pageSize);
            if (index > pageCount)
            {
                index = pageCount;
            }
            else if (index < 1)
            {
                index = 1;
            }
            if (pageCount <= 1)
            {
                return "";
            }
            int startIndex = 1;
            if (pageCount > 9)
            {
                int count = 8;
                int start = index;
                int end = index;
                while (count > 0)
                {
                    if (start > 1)
                    {
                        count--;
                        start--;
                    }
                    if (end < pageCount)
                    {
                        count--;
                        end++;
                    }
                }
                startIndex = start;
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<div class=\"page_line\">");
            sb.AppendLine("<div class=\"Pagination\" id=\"Pagination1\"> ");
            int preIndex = index <= 1 ? 1 : index - 1;
            if (1 < index)
            {
                sb.AppendLine("<A onclick=\"return fenye(" + preIndex + ")\" href=\"javascript:void(0)\">上一页</A>");
            }
            int endIndex = startIndex + 9;
            if (index > 5 && pageCount > 9)
            {
                sb.AppendLine("<A class=firstpage onclick=\"return fenye(1)\" href=\"javascript:void(0)\">1</A>");
                sb.AppendLine("<span class=pagenone >...</span> ");
            }
            for (int i = startIndex; i < pageCount && i < endIndex; i++)
            {
                string cls = "";
                if (i == index)
                {
                    cls = "class=\"currentpage\"";
                }
                if (index <= 5 || i != startIndex)
                {
                    sb.AppendLine("<A " + cls + " onclick=\"return fenye(" + i + ")\" href=\"javascript:void(0)\">" + i + "</A>");
                }
            }
            int next = index < pageCount ? index + 1 : index;
            if (endIndex < pageCount && endIndex > 9)
            {
                sb.AppendLine("<span class=pagenone >...</span> ");
            }
            if (index == pageCount)
            {
                sb.AppendLine("<A class='currentpage' onclick=\"return fenye(" + pageCount + ")\" href=\"javascript:void(0)\">" + pageCount + "</A> ");
            }
            else
            {
                sb.AppendLine("<A class='lastpage' onclick=\"return fenye(" + pageCount + ")\" href=\"javascript:void(0)\">" + pageCount + "</A> ");
            }
            if (index < pageCount)
            {
                sb.AppendLine("<A class=nextpage onclick=\"return fenye(" + next + ")\" href=\"javascript:void(0)\">下一页</A>");
            }
            sb.AppendLine("<span class=\"togopage\">第<input class=\"goinp\" id=\"pageIndex\" type=\"text\" />页 <input name=\"btngo\" type=\"button\" id=\"btngo\" class=\"btngo\" value=\"跳转\"  onclick=\"return GoPage();\" /></span>");
            sb.AppendLine("</div></div>");

            return sb.ToString();
        }

        public static string PageUrlWeb(int recordCount, int index, int pageSize,string url)
        {
            //先计算总页数
            int pageCount = (int)Math.Ceiling((double)recordCount / pageSize);
            if (index > pageCount)
            {
                index = pageCount;
            }
            else if (index < 1)
            {
                index = 1;
            }
            if (pageCount <= 1)
            {
                return "";
            }
            int startIndex = 1;
            if (pageCount > 9)
            {
                int count = 8;
                int start = index;
                int end = index;
                while (count > 0)
                {
                    if (start > 1)
                    {
                        count--;
                        start--;
                    }
                    if (end < pageCount)
                    {
                        count--;
                        end++;
                    }
                }
                startIndex = start;
            }
            //string url = System.Web.HttpContext.Current.Request.RawUrl;
            if (url.IndexOf("?") == -1)
            {
                url = url + "?";
            }
            else if (url.IndexOf("?") > -1)
            {
                var dic = UrlKeyValue(url);
                url = url.Substring(0, url.IndexOf("?") + 1);
                foreach (string key in dic.Keys)
                {
                    if (key != "page")
                    {
                        url += key + "=" + dic[key] + "&";
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<div class=\"pagenum\" style=\"position:absolute; right:12px; bottom:3px;\">");
            int preIndex = index <= 1 ? 1 : index - 1;
            if (1 < index)
            {
                sb.AppendLine("<div class=\"arrow\"><a href='" + url + "page=" + preIndex + "'><img src=\"" + StringTools.GetStaticFile("Images/Train/images/ico_arrowleft.jpg") + "\" /></a></div>");
            }
            else
            {
                sb.AppendLine("<div class=\"arrow\"><a><img src=\"" + StringTools.GetStaticFile("Images/Train/images/ico_arrowleft.jpg") + "\" /></a></div>");
            }
            sb.AppendLine("<ul>");
            int endIndex = startIndex + 9;
            for (int i = startIndex; i < pageCount && i < endIndex; i++)
            {
                string cls = "";
                if (i == index)
                {
                    cls = "class=\"cur\"";
                }
                if (index <= 10 || i != startIndex)
                {
                    sb.AppendLine("<A href='" + url + "page=" + i + "'><li  " + cls + ">" + i + "</li></A>");
                }
            }
            
            int next = index < pageCount ? index + 1 : index;
            if (index == pageCount)
            {
                sb.AppendLine("<A href='" + url + "page=" + pageCount + "'><li class=\"cur\">" + pageCount + "</li></A> ");
            }
            else
            {
                sb.AppendLine("<A href='" + url + "page=" + pageCount + "'><li>" + pageCount + "</li></A> ");
            }
            sb.AppendLine("</ul>");
            if (index < pageCount)
            {
                sb.AppendLine("<div class=\"arrow\"><a href='" + url + "page=" + next + "'><img src=\"" + StringTools.GetStaticFile("Images/Train/images/ico_arrowright.jpg") + "\" /></a></div>");
            }
            else
            {
                sb.AppendLine("<div class=\"arrow\"><a><img src=\"" + StringTools.GetStaticFile("Images/Train/images/ico_arrowright.jpg") + "\" /></a></div>");
            
            }
            sb.AppendLine("</div>");

            return sb.ToString();
        }
        public static Dictionary<string, string> UrlKeyValue(string url)
        {
            var index = url.IndexOf("?");
            if (url.IndexOf("?") == -1)
            {
                return new Dictionary<string, string>();
            }
            else
            {
                string query = url.Substring(index + 1);
                Dictionary<string, string> dic = new Dictionary<string, string>();
                var temps = query.Split('&');
                foreach (var c in temps)
                {
                    var keyvalue = c.Split('=');
                    if (keyvalue.Length == 2)
                    {
                        string key = keyvalue[0];
                        string value = keyvalue[1];
                        if (dic.ContainsKey(key))
                        {
                            dic[key] = dic[key] + "," + value;
                        }
                        else
                        {
                            dic.Add(key, value);
                        }
                    }
                }
                return dic;
            }
        }
        /// <summary>
        /// 替换地址栏参数值
        /// </summary>
        /// <param name="Url">地址</param>
        /// <param name="Key">参数名称</param>
        /// <param name="NewValue">替换值</param>
        /// <returns></returns>
        public static string ChagePara(string Url, string Key, string NewValue)
        {
            string strReturn = "";
            string[] arrUrl = Url.Split('?');
            bool isFindKey = false;
            if (arrUrl.Length < 2)
            {
                strReturn = Url + "?" + Key + "=" + NewValue;
            }
            else
            {
                string[] arrPara = arrUrl[1].Split('&');
                strReturn = arrUrl[0].ToString() + "?";
                foreach (string p in arrPara)
                {
                    if (("," + p + ",").IndexOf("," + Key + "=") > -1)
                    {
                        strReturn += Key + "=" + NewValue + "&"; isFindKey = true;
                    }
                    else
                    {
                        strReturn += p + "&";
                    }
                }
                if (!isFindKey)
                {
                    strReturn += Key + "=" + NewValue;
                }
                else
                {
                    strReturn = strReturn.TrimEnd('&');
                }
            }
            return strReturn;
        }
    }
}
